package ivan.cheng.activitys;

import ivan.cheng.R;
import ivan.cheng.upgrade.UpgradeCheckListener;
import ivan.cheng.upgrade.UpgradeInfo;
import ivan.cheng.utils.LogUtil;
import ivan.cheng.utils.PreferencesUtils;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 更多显示界面
 * 
 * @author tufei
 */
public class MoreInfoActivity extends Activity implements OnClickListener,
		UpgradeCheckListener{

	private RelativeLayout mUpdateLayout;
	private RelativeLayout mShareToLayout;
	private RelativeLayout mNotificationLayout;
	private RelativeLayout mAboutLayout;
	private static final int DIALOG_NO_INTERMINATE = 0;
	private ProgressDialog mProgressDialog = null;
	private TextView mTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.more);

		mTitle = (TextView)findViewById(R.id.custom_title_txt);
		mTitle.setText(R.string.tab_title_more);
		mUpdateLayout = (RelativeLayout) findViewById(R.id.check_update_layout);
		mUpdateLayout.setVisibility(View.GONE);
		mAboutLayout = (RelativeLayout) findViewById(R.id.about_layout);
		mNotificationLayout = (RelativeLayout) findViewById(R.id.notification_switch);
		mShareToLayout = (RelativeLayout) findViewById(R.id.share_to);
		
		mShareToLayout.setOnClickListener(this);
		mNotificationLayout.setOnClickListener(this);
		mUpdateLayout.setOnClickListener(this);
		mAboutLayout.setOnClickListener(this);
		showNotificationToggleStatus();
		//mAboutLayout.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.check_update_layout: 
		{
			showDialog(DIALOG_NO_INTERMINATE);
//			Upgrade upgrade = new Upgrade(this, this);
//			upgrade.CheckIfWeNeedUpgrage(true);
			//mUpdateLayout.setEnabled(false);
		}
			break;
		case R.id.notification_switch:
			 boolean mobileEnable = PreferencesUtils.getBoolValue(this,
						PreferencesUtils.KEY_NOTIFICATION_ON,true);
		        PreferencesUtils.setValue(this,
						PreferencesUtils.KEY_NOTIFICATION_ON,!mobileEnable);
			showNotificationToggleStatus();
			break;
		case R.id.share_to:
			startActivity(new Intent(this,ShareActivity.class));
			break;
		case R.id.about_layout:
			startActivity(new Intent(this, AboutActivity.class));
			break;
		default:
			break;
		}

	}

	/**
     * 显示开关
     * 
     * @param value
     * @param tv
     */
    private void showNotificationToggleStatus() 
    {
        boolean mobileEnable = PreferencesUtils.getBoolValue(this,
				PreferencesUtils.KEY_NOTIFICATION_ON,true);
        TextView tv = (TextView)findViewById(R.id.tv_notification_switch);
        tv.setText(mobileEnable ? R.string.open : R.string.close);
        tv.setBackgroundResource(mobileEnable ? R.drawable.open : R.drawable.close);
        tv.invalidate();
    }
	
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_NO_INTERMINATE: {
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage(getString(R.string.check_update_waiting));
			mProgressDialog.setIndeterminate(true);
			/*
			 * mProgressDialog.setIndeterminateDrawable(getResources()
			 * .getDrawable(R.drawable.loading_progress_anim));
			 */
			mProgressDialog.setCancelable(true);
			return mProgressDialog;
		}
			
		}
		return null;
	}

	@Override
	public void CheckUpgradeComplete(UpgradeInfo ui) 
	{
		mProgressDialog.dismiss();
		mUpdateLayout.setEnabled(true);
		
		int iVersionCode = 0;
		try {
			iVersionCode = this.getPackageManager().getPackageInfo(
					getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			LogUtil.w(e.toString());
		}
		if(null == ui)
		{
			Toast.makeText(this, getString(R.string.check_update_failure), Toast.LENGTH_SHORT).show();
		}
		else if ((ui.result == UpgradeInfo.ON_NEED_UPGRADE || ui.result == UpgradeInfo.FORCE_TO_UPGRADE)
				/** 1.0.0 == 65535 iVersionCode up from 1 */
				&& ui.url != null
				&& Integer.valueOf(ui.available_version) - 65535 > iVersionCode) {
			//sendBroadcast(new Intent(Const.ACTION_SOFTWARE_NEED_UPDATE));

		}
		else
		{
			Toast.makeText(this, getString(R.string.upgrade_newest_version), Toast.LENGTH_SHORT).show();
		}
	}
}
